package com.smartrobot.smartrobot.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.smartrobot.smartrobot.R;

public class TakeIngoupActivity extends Activity implements OnClickListener{
	
	//获取扫描标示
	private final static int SCANNIN_GREQUEST_CODE = 1;
	
	TextView scanQRCode,hairName,hairRealName,hairOrange,hairGroupNum;
	Button takeIn;
    EditText hairResetAccount;
    ImageView back,hairHead;
    LinearLayout hairDetail;
    
    boolean isGetMessage = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        if(savedInstanceState != null){
            throw new NullPointerException();
        }
		this.setContentView(R.layout.page_register_in_group);
		
		init();
		
	}
	
	void init(){
		
		scanQRCode = (TextView)findViewById(R.id.in_group_scanQRCode);
		hairResetAccount= (EditText)findViewById(R.id.in_group_hairAccount);
		takeIn = (Button)findViewById(R.id.in_group_take_in);
		back = (ImageView)findViewById(R.id.in_group_back);
		hairDetail = (LinearLayout)findViewById(R.id.in_group_hairReset_detail);
		hairHead = (ImageView)findViewById(R.id.in_group_hairReset_detail_head);
		hairName = (TextView)findViewById(R.id.in_group_hairReset_detail_name);
		hairRealName = (TextView)findViewById(R.id.in_group_hairReset_detail_real_name);
		hairOrange = (TextView)findViewById(R.id.in_group_hairReset_detail_orange);
		hairGroupNum = (TextView)findViewById(R.id.in_group_hairReset_detail_groupNum);
		
		
		back.setOnClickListener(this);
		scanQRCode.setOnClickListener(this);
		takeIn.setOnClickListener(this);
		hairResetAccount.setOnFocusChangeListener(new ListenFocus());
		
	}

	@Override
	public void onClick(View v) {
		
		if(v.getId() == scanQRCode.getId()){
			
			Intent intent = new Intent();
			intent.setClass(TakeIngoupActivity.this, MipcaActivityCapture.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivityForResult(intent, SCANNIN_GREQUEST_CODE);
			
		}
		
		if(v.getId() == takeIn.getId()){
			
			if(hairResetAccount.getText().toString().length() < 11 ||
					hairResetAccount.getText().toString().length()>11){
				Toast.makeText(TakeIngoupActivity.this, "理发师账号有误！", Toast.LENGTH_SHORT).show();
			}else{
				Toast.makeText(TakeIngoupActivity.this, "加入成功！", Toast.LENGTH_SHORT).show();
			}
		}
		
		if(v.getId() == back.getId())
			this.finish();
		
		
		
	}
	
	class ListenFocus implements OnFocusChangeListener{

		//监听失去焦点的时候获取 店主详细信息
		public void onFocusChange(View v, boolean hasFocus) {
			
			if(!hasFocus)
			if(hairResetAccount.getText().toString().length() == 11){
				if(!isGetMessage){
					System.out.println("去服务器获取消息！！");
					hairDetail.setVisibility(View.VISIBLE);
					isGetMessage = true;
				}
				
			}
			
		}
		
	}
	
	@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
		case SCANNIN_GREQUEST_CODE:
			if(resultCode == RESULT_OK){
				Bundle bundle = data.getExtras();
				
				Toast.makeText(TakeIngoupActivity.this, "二维码扫描的内容--->     "+bundle.getString("result"), Toast.LENGTH_SHORT).show();
			}
			break;
		}
    }
	
	@Override  
    public boolean onTouchEvent(MotionEvent event) {  
        // TODO Auto-generated method stub  
        if (event.getAction() == MotionEvent.ACTION_DOWN) {  
            System.out.println("down");  
            if (this.getCurrentFocus() != null) {  
                if (this.getCurrentFocus().getWindowToken() != null) {
                	hairResetAccount.clearFocus();
                	InputMethodManager imm= (InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),  
                            InputMethodManager.HIDE_NOT_ALWAYS);  
                }  
            }  
        }  
        return super.onTouchEvent(event);  
    }
	
}


